<?php

namespace App\Models;

use App\Service\Tree;

class SiteType extends Base
{

    // 专栏作家
    protected $table = 'site_type';

    // 缓存字段，用于搜索的字段
    protected $cacheFields = ['id',];

    // 获取所有的话题信息
    public function getAll($where = [], $order = 'rank ASC')
    {
        // 获取缓存
        $bang     = $this->bang;
        $is_cache = $this->is_cache || $bang;
        $args     = compact('where');
        if($is_cache){
            $cacheName = $this->cacheKey('lists', $args);
            $cacheData = $this->cacheClass()->getCache($cacheName);
            if(!empty($cacheData)){
                // 检查是否已经绑定
                if(($cacheData) && (!empty($cacheData))){
                    $this->checkBangList($cacheData, $cacheName);
                }

                return $cacheData;
            }
        }

        // 查询
        if(is_array($order)){
        } else{
            $order = explode(" ", $order);
        }

        $lists = $this->where($where)->get()->toArray(); // ->orderby($order[0], $order[1])
        $lists = array_column($lists, null, 'id');
        // 保存为缓存
        if($is_cache){
            $this->addCacheList($lists, $cacheName, $lists, 'list');
        }

        return $lists;
    }

    // 身份类型
    public function userType()
    {
        $where  = [['channel', 'in', ['user_type', 'organ_type']]];
        $where1 = ['channel' => 'user_type'];
        $where2 = ['channel' => 'organ_type'];
        $where3 = ['channel' => 'user_medium'];
        $where4 = ['channel' => 'user_expert'];
        /*$lists1 = $this->isCache(true)->getAll($where1);
        $lists2 = $this->isCache(true)->getAll($where2);
        $lists3 = $this->isCache(true)->getAll($where3);
        $lists4 = $this->isCache(true)->getAll($where4);*/
        $lists1 = $this->bangField(['channel'])->getAll($where1);
        $lists2 = $this->bangField(['channel'])->getAll($where2);
        $lists3 = $this->bangField(['channel'])->getAll($where3);
        $lists4 = $this->bangField(['channel'])->getAll($where4);
        $lists  = array_merge($lists1, $lists2, $lists3, $lists4);
        $lists  = array_column($lists,null,'id');

        return $lists;
    }

    // 定义验证操作
    public function rules()
    {

        $add = [
        ];

        $edit = [
        ];

        $info = [
        ];


        $rule = [
            'name'    => 'required',
            'channel' => 'required',
            'rank'    => 'numeric',
        ];

        $msg = [
            'id'      => '编号',
            'rank'    => '排序',
            'channel' => '类型',
            'name'    => '栏目名称',
            'ctime'   => '创建时间',
            'mtime'   => '修改时间',
        ];

        return ['rule' => $rule, 'msg' => $msg, 'add' => $add];
    }
}
